#coding=utf-8

from uliweb.orm import *
from uliweb.utils.common import get_var

class Message(Model):
    __verbose_name__ = u'消息'
    __dispatch_enabled__ = False    
    type =  Field(CHAR, verbose_name='消息类别', max_length=1, choices=get_var('PARA/MESSAGE_TYPE'))   
    memo = Field(TEXT, verbose_name='内容')
    user = Reference('user', verbose_name='用户', collection_name='user_systems')
    read_flag = Field(bool, verbose_name='已读')
    create_date = Field(datetime.datetime, verbose_name='创建时间', auto_now=True, auto_now_add=True)
    sender = Reference('user', verbose_name='创建人')

    def __unicode__(self):
        return u'%s-%s(%s)' % (self.memo)
    def get_url(self):
        return u'<a href="/messages/%d" title="%s">%s</a>' % (self.id)
            
    class Table:
        fields = [
            {'name':'memo', 'width':500},
            {'name':'type',  'width':70},
            {'name':'user',  'width':70},
            {'name':'read_flag',  'width':70},
            {'name':'create_date',  'width':80},
            {'name':'sender', 'width':80},
        ]
    
    @classmethod
    def OnInit(cls):
        Index('msg_idx', cls.c.user, cls.c.read_flag)
    